<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.projectName" clearable placeholder="项目名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select
          v-model="query.deployStatus"
          class="filter-item"
          clearable
          placeholder="请选择部署状态"
          style="width: 200px;"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.deploy_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <date-range-picker v-model="query.deployTime" class="date-item" />
        <rrOperation :crud="crud" />
      </div>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @row-click="rowClick" @selection-change="crud.selectionChangeHandler">
        <el-table-column prop="projectName" label="项目名称" width="150" />
        <el-table-column prop="sourceBranch" label="源代码分支" width="250" />
        <el-table-column prop="targetBranch" label="目标分支" width="180" />
        <el-table-column prop="finishStatus" label="状态" width="150">
          <template slot-scope="scope">
            <i v-if="scope.row.finishStatus==='部署中'" class="el-icon-loading" />
            <el-tag :type="getFinishStatusType(scope.row.finishStatus)">
              {{ scope.row.finishStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="operatorName" label="部署人" width="80" />
        <el-table-column prop="deployTime" label="部署时间" width="200" />
        <el-table-column prop="deployStage" label="部署流程">
          <template slot-scope="scope">
            <el-steps :active="scope.row.stage" :process-status="scope.row.processStatus" finish-status="success">
              <el-step title="配置" />
              <el-step title="构建" />
              <el-step title="构建检测" />
              <el-step title="部署" />
              <el-step title="健康检测" />
            </el-steps>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import crudDeployRec from '@/api/deploy/deployRec'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import global_const from '@/mixins/global_const'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, env: null, projectId: null, projectName: null, stage: null, sourceBranch: null, targetBranch: null, buildTplId: null, buildQueueId: null, buildParam: null, buildLog: null, deployParam: null, deployTplId: null, operatorId: null, deployTime: null, deployStatus: null }
export default {
  name: 'DeployRec',
  components: { pagination, rrOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud(), global_const],
  dicts: ['deploy_stage', 'deploy_status'],
  cruds() {
    return CRUD({ title: '部署记录', url: 'api/deployRec/query2', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDeployRec }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'deployRec:add'],
        edit: ['admin', 'deployRec:edit'],
        del: ['admin', 'deployRec:del']
      },
      rules: {
        env: [
          { required: true, message: '环境不能为空', trigger: 'blur' }
        ],
        projectId: [
          { required: true, message: '项目id不能为空', trigger: 'blur' }
        ],
        projectName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        stage: [
          { required: true, message: '部署阶段不能为空', trigger: 'blur' }
        ],
        buildTplId: [
          { required: true, message: '构建模板id不能为空', trigger: 'blur' }
        ],
        deployTplId: [
          { required: true, message: '部署模板id不能为空', trigger: 'blur' }
        ],
        deployStatus: [
          { required: true, message: '部署状态不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'env'
    ])
  },
  mounted() {
    this.$bus.$on('global-env-change', data => {
      this.crud.toQuery()
    })
  },
  beforeDestroy() {
    this.$bus.$off('global-env-change')
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.query['env'] = this.env
      return true
    },
    [CRUD.HOOK.afterRefresh]() {
      const data = []
      this.crud.data = this.crud.data.forEach(item => {
        item['processStatus'] = this.processStatus(item.deployStatus)
        item['finishStatus'] = this.finishStatus(item.deployStatus, item.stage)
        data.push(item)
      })
      this.crud.data = data.filter(item => item)
    },
    rowClick(row) {
      this.$router.push(`/deploy/proccess?projectId=${row.projectId}&deployRecId=${row.id}`)
    },
    getFinishStatusType(finishStatus) {
      if (finishStatus === '部署完成') {
        return 'success'
      } else if (finishStatus === '部署失败') {
        return 'danger'
      } else if (finishStatus === '部署中') {
        return 'warning'
      }
      return 'info'
    }
  }
}
</script>

<style scoped>

</style>
